# -*- coding: utf-8 -*-
"""
Created on 2021-12-16 15:46:07
---------
@summary:
---------
@author: 74793
"""
import time
import requests
from fake_useragent import UserAgent
from feapder.db.mysqldb import MysqlDB
# 线程池
from multiprocessing.dummy import Pool

class Proxy():

    def __init__(self):
        self.db = MysqlDB
        self.url = "http://httpbin.org/get"
        self.headers = {'User-Agent': UserAgent().random}
        self.proxy_ip = []

    def get_ips(self):
        """获取 ip，并返回一个列表 """
        job_list = []
        db = MysqlDB()
        sql = """select ip from ippool"""
        ips = db.find(sql)
        for ip in ips:
            job_list.append(ip[0])

        return job_list

    def useful_ips(self):
        """获取 ip，并返回一个列表 """
        job_list = []
        db = MysqlDB()
        sql = """select ip from ippool where status = 1"""
        ips = db.find(sql)
        for ip in ips:
            job_list.append(ip[0])

        return job_list

    def test_ip(self, ip):
        """测试 ip 是否有效 """
        proxies = {
            'http': 'http://{}'.format(ip),
            'https': 'https://{}'.format(ip),
        }
        try:
            resp = requests.get(url=self.url, proxies=proxies,
                                headers=self.headers, timeout=3)
            if resp.status_code == 200:
                print(ip, '\033[31m可用\033[0m')
                self.proxy_ip.append(ip)
            else:
                pass
        except Exception as e:
            pass

    def update_ip(self, ip):
        """测试 ip 是否有效 """
        proxies = {
            'http': 'http://{}'.format(ip),
            'https': 'https://{}'.format(ip),
        }
        db = MysqlDB()
        sql = """update ippool set status=1 where ip = '{}' """.format(ip)
        sql2 = """update ippool set status=0 where ip = '{}' """.format(ip)
        try:
            resp = requests.get(url=self.url, proxies=proxies,
                                headers=self.headers, timeout=3)
            if resp.status_code == 200:
                # print(sql)
                db.update(sql)
                print(ip, '\033[31m可用\033[0m')
            else:
                # print("else", sql2)
                db.update(sql2)
        except Exception as e:
            # print("检测失败", sql2, e)
            db.update(sql2)



    def return_ips(self):
        """给调用者返回一个代理 ip池 列表数据"""
        pool = Pool(8)
        pool.map(self.test_ip, self.get_ips())
        if len(self.proxy_ip) >= 5:

            return self.proxy_ip

    def update2(self):
        """批量快速检测ip并改变状态"""
        pool = Pool(8)
        pool.map(self.update_ip, self.get_ips())

if __name__ == '__main__':

    proxy = Proxy()
    # print(proxy.return_ips())
    while(True):
        proxy.update2()
        print("休眠中(100s)...")
        time.sleep(100)